<?php
/*
	提现
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
global $_RQ;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
cfc('core')->checklogin();
$return=array();
$open=model('core/member/cash')->check_open();
$openred=model('core/member/cash')->check_open(true);
if(!$open && !$openred){
	exi('系统不支持提现！','','goback');
}
$setting=uni_setting('cash');
$setting['percent']=round($setting['percent'],2);
if($_RQ['op']=='get'){
	$member=pdo_get('core_members',array('id'=>$_SESSION['uid']),'credit2');
	$member['ideal_money']=pdo_getcolumn('core_ideal_money',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'money');
	$member['ideal_money']=$member['ideal_money']?$member['ideal_money']:'0.00';
	$return['item']=$member;
	$return['item']['banklist']=array();
	$setting['type_array']=array('提现到微信钱包','提现到支付宝','提现到银行卡');
	$return['setting']=$setting;
	if(!$open || $_RQ['fee_type']==1){
		$return['post']['fee_type']=1;
	}
	$return['post']['type']='3';
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	if($post['type']==1){
		if(!$post['realname']){
			exi('请输入姓名','error');
		}
		if(!$post['alipay']){
			exi('请输入支付宝帐号','error');
		}
		if(!$post['alipay1']){
			exi('请确认帐号','error');
		}
		if($post['alipay1']!=$post['alipay']){
			exi('确认帐号与支付宝帐号不匹配！','error');
		}
	}
	if($post['type']==2){
		if(!$post['realname']){
			exi('请输入姓名','error');
		}
		if(!$post['bankname']){
			exi('请输入所属银行','error');
		}
		if(!$post['bankcard']){
			exi('请银行卡号','error');
		}
		if(!$post['bankcard1']){
			exi('请确认卡号','error');
		}
		if($post['bankcard']!=$post['bankcard1']){
			exi('确认卡号与银行卡号不匹配！','error');
		}
	}
	$fee=$post['fee'];
	if($post['type']!=3){
		$basic=$this->plugin_setting('basic');
		if($basic['is_tixianfee']==1){
			$isti=pdo_getsum('core_member_cash',array('mid'=>$_SESSION['uid'],'status <'=>2),'money');
			$ischong=pdo_getsum('core_recharge',array('mid'=>$_SESSION['uid'],'status'=>1),'fee');
			$isorder=pdo_getsum('jy_weishop_order',array('status'=>array(1,2,3),'plugin_name'=>'jy_psipingtuan','mid'=>$_SESSION['uid'],'is_team'=>1),'price');
			$ischong=$ischong?:0;
			$isti=$isti?:0;
			$isorder=$isorder?:0;
			$lastfee=$ischong-$isti-$isorder;
			if($lastfee<$fee){
				exi('您一共充值'.$ischong.'元，已拼中金额'.$isorder.'元，已提现金额'.$isti.'元，还可提现金额'.$lastfee.'元','error');
			}
		}
		model('core/member/cash')->check_cash_fee($_SESSION['uid'],$fee);
		$cash=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'ordersn'=>get_ordersn('core_member_cash','ordersn','CASH'),
			'money'=>$fee,
			'type'=>intval($post['type']),
			'realname'=>$post['realname'],
			'alipay'=>$post['alipay'],
			'bankname'=>$post['bankname'],
			'bankcard'=>$post['bankcard'],
			'service_money'=>round($fee*$setting['percent']/100,2),
			'createtime'=>TIMESTAMP,
			'status'=>0,
		);
		$cash['cash_money']=$cash['money']-$cash['service_money'];
		pdo_insert('core_member_cash',$cash);
		$id=pdo_insertid();
		if(model('core/member/cash')->setting['check'] || $cash['type'] !=0){
			exi('申请提现成功，请等待管理员审核！','','goback');
		}else{
			model('core/member/cash')->pay($id);
			exi('提现成功！','success','goback');
		}
	}else{
		$member=model('core/member')->detail($_SESSION['uid'],array('credit2'));
		if(!$member){
			exi('提现失败，指定会员不存在！','error');
		}
		if($member['credit2']<$fee){
			exi('提现失败，会员账户余额不足！当前余额：'.$member['credit2'],'error');
		}
		model('core/member')->cut_credit2($_SESSION['uid'],$fee,'提现到保证金','core');
		$this->model('member')->recharge($_SESSION['uid'],$fee,0,'提现充值保证金');
	}
	exi('提现成功！','success','goback');
}
if($_RQ['op']=='redpost'){
	$post=get_postdata();
	if(!$post['fee']){
			exi('请输入金额','error');
	}
	if(!$post['to_type']){
			exi('请选择提现方式','error');
	}
	$fee=$post['fee'];
	if(is_numeric($fee)){
	}else{
		exi('提现提现红包必须是数字！','error');
	}
	$service_money=round($fee*$setting['percent']/100,2);
	$service_money=intval($post['to_type'])==1?0:$service_money;
	$params=array(
		'mid'=>$_SESSION['uid'],
		'to_type'=>intval($post['to_type']),
		'realname'=>$post['realname'],
		'alipay'=>$post['alipay'],
		'bankname'=>$post['bankname'],
		'bankcard'=>$post['bankcard'],
		'money'=>$fee,
		'cash_money'=>$fee-$service_money
	);
	$cash_id=model('core/red_pack')->cash($params);
	if(model('core/member/cash')->setting['check'] || $params['to_type'] !=0){
		exi('申请提现成功，请等待管理员审核！','','goback');
	}else{
		model('core/red_pack')->pay_cash($cash_id);
		exi('提现成功！','success','goback');
	}
}